import urllib.request
from lxml import etree
# 请求对象的定制
# 获取网页的源码
# 下载
# https://sc.chinaz.com/tupian/qinglvtupian_3.html

def create_request(page):
    if page == 1:
        url = 'https://sc.chinaz.com/tupian/qinglvtupian.html'
    else:
        url = 'https://sc.chinaz.com/tupian/qinglvtupian_'+str(page)+'.html'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36'
    }
    request = urllib.request.Request(url, headers=headers)
    return request


def getContent(request):
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    return content

def down_load(content):
    # urllib.request.urlretrieve('','')
    # print(content)
    tree = etree.HTML(content)
    name_list = tree.xpath("//div[contains(@class,'tupian-list')]//img/@alt")
    src_list = tree.xpath("//div[contains(@class,'tupian-list')]//img/@data-original")
    for i in range(len(name_list)):
        src = src_list[i]
        name = name_list[i]
        url = 'https:'+ src
        # print(url.rfind('.'))
        urllib.request.urlretrieve(url=url, filename='./loveImg/'+name + '.'+url[url.rfind(".")+1:])

if __name__ == '__main__':
    start_page = int(input("请输入起始页码"))
    end_page = int(input("请输入结束页码"))
    for page in range(start_page, end_page+1):
        request = create_request(page)
        content = getContent(request)
        down_load(content)